<?php
include_once '../Login/databaseconnection.php';
$Role=$_POST['Role'];
 if($Role=="")
{
$Role="Teacher";
}
  $T_NIC_Number=$_POST['T_NIC_Number'];
   $School_Name=$_POST['School_Id'];
   $sch_name=mysql_query("SELECT * FROM school WHERE Name='$School_Name'");
if($sn=mysql_fetch_array($sch_name))
{
$School_Id=$sn['School_Id'];
}
   $First_Name=$_POST['First_Name'];
  $Last_Name=$_POST['Last_Name'];
   $Date_Of_Birth=$_POST['Date_Of_Birth'];
   $Gender=$_POST['Gender'];
   $Nature_Of_Appointment=$_POST['Nature_Of_Appointment'];
   $Service=$_POST['Service'];
   $Specialised_In=$_POST['Specialised_In'];
 $New_Specialised_In=$_POST['New_Specialised_In'];
 $reg_Specia_In="";
 for($i=0;$i<sizeof($Specialised_In);$i++)
  {
  if(empty($reg_Specia_In))
  {
  $reg_Specia_In=$Specialised_In[$i];
  }
  else
  {
  $reg_Specia_In=$reg_Specia_In.",".$Specialised_In[$i];
  }
  }
  if($New_Specialised_In!="")
  {
  if(empty($reg_Specia_In))
  {
  $reg_Specia_In=$New_Specialised_In;
  }
  else
  {
  $reg_Specia_In=$reg_Specia_In.",".$New_Specialised_In;
  }
  }
  if(empty($reg_Specia_In))
  {
  $reg_Specia_In="";
  }
if($Service=="")
{
$serv="0year";
}
if($Service!="")
{
$chars = str_split( $Service );
  $i=0;
while($i<strlen($Service))
{
$c=$chars[$i]; 
if($c=="y"||$c=="Y")
{
$st=$i;
break;
}
else
{
$i++;
}
}
if($st>0&&$st<strlen($Service))
{
$ser=substr($Service,0,$st);
}
if($st==0)
{
$ser="0";
}
if($st=="")
{
$ser=$Service;
}
if((int) $ser>1)
{
$serv=$ser."years";
}
if((int) $ser<=1)
{
$serv=$ser."year";
}
}
  $Academic_Qualifications=$_POST['Academic_Qualifications'];
  $Professional_Qualifications=$_POST['Professional_Qualifications'];
	 $Profe_Qualifications= mysql_real_escape_string($Professional_Qualifications);
  $Achievements=$_POST['Achievements_Awards'];
  $Achieve= mysql_real_escape_string($Achievements);
 $Address=$_POST['Address'];
  $Mobile_Tp_Number=$_POST['Mobile_Tp_Number'];
   $Home_Tp_Number=$_POST['Home_Tp_Number'];
   $Office_Tp_Number=$_POST['Office_Tp_Number'];
  $Appointment_Date=$_POST['Appointment_Date'];
 $Appointed_Subject=$_POST['Appointed_Subject'];
  $T_NIC_Numbero=$_SESSION['$T_NIC_Number'];

    $dehol="";
	$_SESSION['$T_NIC']=$_SESSION['$T_NIC_Number'];
	date_default_timezone_set("Europe/Paris");
    $t_d=date("Y/m/d");
	$pervious=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$T_NIC_Numbero'");
	if($pervious)
	{
	if($per_row=mysql_fetch_array($pervious))
	{
$Rolep=$per_row['Role'];
  $Mobile_Tp_Numberp=$per_row['Mobile_Tp_Number'];
  $Home_Tp_Numberp=$per_row['Home_Tp_Number'];
  $Office_Tp_Numberp=$per_row['Office_Tp_Number'];
 $Achievementsp=$per_row['Achievements'];
	}
	}
	if($T_NIC_Number==""||$School_Id==""||$First_Name==""||$Last_Name==""||$Date_Of_Birth==""||$Gender==""||$Nature_Of_Appointment==""||$Service==""||$Academic_Qualifications==""||$Address==""||$Appointment_Date==""||$Appointed_Subject=="")
	{
	$_SESSION['errorin']="Not enough inputs!";
	 }
	if($T_NIC_Number!=""&&$School_Id!=""&&$First_Name!=""&&$Last_Name!=""&&$Date_Of_Birth!=""&&$Gender!=""&&$Nature_Of_Appointment!=""&&$Service!=""&&$Academic_Qualifications!=""&&$Address!=""&&$Appointment_Date!=""&&$Appointed_Subject!="")
	{
	  if(($Achievementsp!=""&&$Achievements=="")||($Office_Tp_Numberp!=""&&$Office_Tp_Number=="")||($Home_Tp_Numberp!=""&&$Home_Tp_Number=="")||($Mobile_Tp_Numberp!=""&&$Mobile_Tp_Number=="")||($Rolep!=""&&$Role==""))
  {
   $_SESSION['errorempt']="You have deleted some values If you want to put them,Please update again!";
  }
	for($i=0;$i<sizeof($Academic_Qualifications);$i++)
  {
   if($Academic_Qualifications[$i]=="Degree_holder")
  {
 $dehol="yes";
  $deg=$_POST['Academic_Qualifications_Deg'];
   $degree=mysql_real_escape_string($deg);
    if($degree=="")
  {
  $_SESSION['errordeg']="Please type your subject you got degree on it!";
  }
  else
  {
  if(empty($A_Qua))
  {
  $A_Qua="deg_".$degree;
  }
  else
  {
  $A_Qua=$A_Qua.",deg_".$degree;
  }
  }
  }
  else
  {
  if(empty($A_Qua))
  {
  $A_Qua=$Academic_Qualifications[$i];
  }
  else
  {
$A_Qua=$A_Qua.','.$Academic_Qualifications[$i];
}
}
}
if($dehol=="yes")
  {
  $deg=$_POST['Academic_Qualifications_Deg'];
   $degree=mysql_real_escape_string($deg);
    if($degree!="")
  {
  $sql=mysql_query("UPDATE teacher SET Specialised_In='$reg_Specia_In',Role='$Role',T_NIC_Number='$T_NIC_Number',School_Id='$School_Id',First_Name='$First_Name',Last_Name='$Last_Name',Date_Of_Birth='$Date_Of_Birth',Gender='$Gender',Nature_Of_Appointment='$Nature_Of_Appointment',Service='$serv',Academic_Qualifications='$A_Qua',Professional_Qualifications='$Profe_Qualifications',Address='$Address',Mobile_Tp_Number='$Mobile_Tp_Number',Home_Tp_Number='$Home_Tp_Number',Office_Tp_Number='$Office_Tp_Number',Appointment_Date='$Appointment_Date',Appointed_Subject='$Appointed_Subject',Last_Updated_Date='$t_d',Achievements_Awards='$Achieve' WHERE T_NIC_Number='$T_NIC_Numbero'");
if($sql)
  {
$_SESSION['error']="Your record is successfully updated!";
  }

  if(!$sql)
  {
 $_SESSION['error']="Sorry unable to update your record!";
  }
	}
}
else
	{
	$sql=mysql_query("UPDATE teacher SET Specialised_In='$reg_Specia_In',Role='$Role',T_NIC_Number='$T_NIC_Number',School_Id='$School_Id',First_Name='$First_Name',Last_Name='$Last_Name',Date_Of_Birth='$Date_Of_Birth',Gender='$Gender',Nature_Of_Appointment='$Nature_Of_Appointment',Service='$serv',Academic_Qualifications='$A_Qua',Professional_Qualifications='$Profe_Qualifications',Address='$Address',Mobile_Tp_Number='$Mobile_Tp_Number',Home_Tp_Number='$Home_Tp_Number',Office_Tp_Number='$Office_Tp_Number',Appointment_Date='$Appointment_Date',Appointed_Subject='$Appointed_Subject',Last_Updated_Date='$t_d',Achievements_Awards='$Achieve' WHERE T_NIC_Number='$T_NIC_Numbero'");
if($sql)
  {
$_SESSION['error']="Your record is successfully updated!";
  }

  if(!$sql)
  {
 $_SESSION['error']="Sorry unable to update your record!";
  }
	}
}
  ?>
